package com.xbongbong.paas.enums;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.clazz.ConditionClass;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * @author: wufeng
 * @date: 2018/9/5 16:09
 * @desrcption: 条件语句枚举类
 */
public enum ConditionEnum {

    // TODO 地址判断暂时去掉
    /**
     * 属于
     */
//    BELONG("all", "all", "属于"),
//    /**
//     * 不属于
//     */
//    NOBELONG("noall", "noall", "不属于"),

    /**
     * 等于
     */
    EQUAL("equal", "=", "enum.condition.memo.equal"),
    /**
     * 不等于
     */
    NOEQUAL("noequal", "<>", "enum.condition.memo.noequal"),
    /**
     * 包含任意一个
     */
    INCLUDE("include", " include ", "enum.condition.memo.include"),
    /**
     * 同时包含
     */
    ALLINCLUDE("allinclude", " allinclude ", "enum.condition.memo.allinclude"),
    /**
     * 包含
     */
    LIKE("like", " like ", "enum.condition.memo.like"),
    /**
     * 不包含
     */
    NOLIKE("nolike", " nolike ", "enum.condition.memo.nolike"),
    /**
     * 等于任意一个
     */
    IN("in", "in", "enum.condition.memo.in"),
    /**
     * 不等于任意一个
     */
    NOIN("noin", "noin", "enum.condition.memo.noin"),
    /**
     * 大于等于
     */
    GREATEREQUAL("greaterequal", " >= ", "enum.condition.memo.greaterequal"),
    /**
     * 大于
     */
    GREATERTHAN("greatethan", ">", "enum.condition.memo.greaterthan"),
    /**
     * 小于等于
     */
    LESSEQUAL("lessequal", " <= ", "enum.condition.memo.lessequal"),
    /**
     * 小于
     */
    LESSTHAN("lessthan", "<", "enum.condition.memo.lessthan"),
    /**
     * 大于等于某个值，并且小于某个值 （遵从左闭右开原则）
     */
    RANGE("range", "between", "enum.condition.memo.range"),
    /**
     * 大于等于某个值，并且小于某个值
     */
    NORANGE("norange", "notbetween", "enum.condition.memo.norange"),
    /**
     * 为空
     */
    EMPTY("empty", " == ''", "enum.condition.memo.empty"),
    /**
     * 不为空
     */
    NOEMPTY("noempty", "!= ''", "enum.condition.memo.noempty"),

    // 以下BI专用
    /**
     * 常用
     */
    NORMAL("normal", "normal", "enum.condition.memo.normal"),
    /**
     * 自定义动态
     */
    CUSTOM("custom", "custom", "enum.condition.memo.custom"),
    /**
     * 动态值
     */
    DYNAMIC("dynamic", "dynamic", "enum.condition.memo.dynamic"),
    ;


    /**
     * 条件
     */
    private String symbol;

    /**
     * 将条件替换成符号
     */
    private String replaceSymbol;

    /**
     * 描述
     */
    private String memo;


    ConditionEnum(String symbol, String replaceSymbol, String memo){
        this.symbol = symbol;
        this.replaceSymbol = replaceSymbol;
        this.memo = memo;
    }

    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public String getReplaceSymbol() {
        return replaceSymbol;
    }

    public void setReplaceSymbol(String replaceSymbol) {
        this.replaceSymbol = replaceSymbol;
    }

    public String getMemo() {
        return I18nMessageUtil.getMessage(memo);
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

    public static ConditionEnum getConditionEnum(String symbol){
        for (ConditionEnum condition: values()) {
            if(condition.getSymbol().equals(symbol)){
                return condition;
            }
        }
        return null;
    }

    public static String getMemoBySymbo(String symbol) {
        for (ConditionEnum condition: values()) {
            if(condition.getSymbol().equals(symbol)){
                return condition.getMemo();
            }
        }
        return "";
    }

    /**
     * 获取文件的搜索条件列表
     * @return
     */
    public static List<ConditionClass> getConditionList(String[] arr){
        HashSet<String> hashSet = new LinkedHashSet<>();
        // 减少for循环，减少时间复杂度
        Collections.addAll(hashSet, arr);
        List<ConditionClass> list = new ArrayList<>();
        for (ConditionEnum condition: values() ) {
            if(hashSet.contains(condition.getSymbol())){
                ConditionClass conditionClass = new ConditionClass();
                conditionClass.setMemo(condition.getMemo());
                conditionClass.setReplaceSymbol(condition.getReplaceSymbol());
                conditionClass.setSymbol(condition.getSymbol());
                list.add(conditionClass);
            }

        }
        return list;
    }
}
